package com.furniture.ui.utils.data;

import android.os.Environment;
import android.util.Log;

import com.furniture.sql.Good;
import com.furniture.ui.UpdateUi;
import com.furniture.ui.utils.ListToJson;
import com.furniture.ui.utils.WriteInFile;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.util.Auth;

import org.json.JSONObject;

import java.io.File;
import java.util.List;

/**
 * Created by Bob on 2016/3/28.
 */
public class QiNiuUpLoad {
    private  final  static  String ACCESS_KEY="TVHls2kvLimNcR93cj-NvqWOxsmZ5IXiDAYN8UD0";
    private final static String SECRET_KEY="59zr_6bCTXTcFkjYtU5j4EFsfIfErjGCoDDuUEB5";
    private static final String BUCKET="furniture";
    public static File dir = new File(Environment.getExternalStorageDirectory(), "Furniture/goodList.json");
    public static File photo;
    private static Auth auth;
    private static BucketManager bucketManager;
    private WriteInFile writeInFile = new WriteInFile();

    //获取服务端taken
    public String getUploadToken() {
        auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String token = auth.uploadToken(BUCKET);

//        Log.e("qiNiuToken", token);
        return token;
    }

    /**
     * 七牛删除
     * @param key
     */
    public  void deleteJson(String key){
        try {
            bucketManager = new BucketManager(auth);
            bucketManager.delete(BUCKET, key);
            Log.i("qiniu", "access");
        } catch (QiniuException e) {
            e.printStackTrace();
//            Log.e("qiniu",e.toString());
        }
    }

    /**
     * 七牛上传
     * @param key
     * @param mData
     */
    public QiNiuUpLoad(String key, List<Good> mData){                   //传入文件名
        // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
        UploadManager uploadManager = new UploadManager();
        String token = getUploadToken();
        deleteJson(key);                                //删除七牛上的json
        String msg = ListToJson.listToJson(mData);      //获取本地数据库中的json数据
        writeInFile.initData(msg);                      //写入json数据到本地文件
        uploadManager.put(dir, key, token,              //上传本地文件到七牛
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        //  res 包含hash、key等信息，具体字段取决于上传策略的设置。
                        Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                    }
                }, null);
    }

    public QiNiuUpLoad(String key, final UpdateUi ui){                   //传入文件名
        // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
        UploadManager uploadManager = new UploadManager();
        String token = getUploadToken();
        photo = new File(Environment.getExternalStorageDirectory(), "DCIM/"+key);
        uploadManager.put(photo, key, token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        //  res 包含hash、key等信息，具体字段取决于上传策略的设置。
                        Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                        ui.onUpdate();
                    }
                }, null);
    }
}
